package tech.aistar.dao.impl;

import tech.aistar.dao.IUserDao;
import tech.aistar.entity.User;

import java.sql.*;
import java.util.List;

/**
 * 本类用来演示: 实现类
 *
 * @author: success
 * @date: 2022/8/11 9:41 上午
 */
@SuppressWarnings("all")
public class UserDaoImpl implements IUserDao {
    @Override
    public int save(User user) {
        //1. 加载驱动 - 可以省略.
        Connection conn = null;
        Statement st = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2. 获取连接
            //准备连接参数
            String url = "jdbc:mysql://localhost:3306/dy?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
            String username = "root";
            String password = "root";

            conn = DriverManager.getConnection(url, username, password);

            //3. 创建语句对象 - 负责发送sql到db-serve端
            st = conn.createStatement();

            //4. 发送sql语句,准备一条sql语句
            //  "insert into jdbc_user(username,password,birthday,power) values('"+admin+"','"+123+"','"+2021-01-01+"',"+0+")";
            //java.util.Date -> String
            String bt = String.format("%tF",user.getBirthday());
            String sql = "insert into jdbc_user(username,password,birthday,power) values('"+user.getUsername()+"','"+user.getPassword()+"','"+bt+"',"+user.getPower()+")";
            //System.out.println(sql);
            return st.executeUpdate(sql);//DML操作
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6. 释放资源,注意顺序,最后创建的资源需要先释放

            if(null!=st){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(null!=conn){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    @Override
    public User findByUsername(String username) {
        User user = null;
        //1. 加载驱动 - 可以省略.
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2. 获取连接
            //准备连接参数
            String url = "jdbc:mysql://localhost:3306/dy?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
            String userName = "root";
            String password = "root";

            conn = DriverManager.getConnection(url, userName, password);

            //3. 创建语句对象 - 负责发送sql到db-serve端
            st = conn.createStatement();

            //4. 发送sql语句,准备一条sql语句
            String sql = "select * from jdbc_user where username='"+username+"'";
            System.out.println(sql);
            rs = st.executeQuery(sql);//传入dql语句.

            //5. 处理结果集
           if(rs.next()){//向下移动一行,如果下一行没有对应的行记录,直接返回false
                Integer id = rs.getInt(1);
                String uname = rs.getString(2);
                String pwd = rs.getString(3);
                //java.sql.Date extends java.util.Date
                Date birthdate = rs.getDate(4);
                Integer power = rs.getInt(5);

                //将这些多个不同的数据类型的数据 -> 对象 - 数据在内存中的载体
                user = new User(uname,pwd,birthdate,power);
                user.setId(1);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6. 释放资源,注意顺序,最后创建的资源需要先释放
            if(null!=rs){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(null!=st){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(null!=conn){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return user;
    }
}
